package com.quantdo.demo.edit;


import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorPart;

import com.quantdo.client.Activator;
import com.quantdo.demo.date.TableDate;
import com.quantdo.demo.date.TableDateFactory;
import com.quantdo.demo.dialog.ModifyPatientInfoDialog;

public class NavigatorEdit extends EditorPart{

	private TableViewer tableViewer;
	private boolean sort = true;
	
	@Override
	public void doSave(IProgressMonitor arg0) {
	}

	@Override
	public void doSaveAs() {
	}

	@Override
	public void init(IEditorSite arg0, IEditorInput arg1) throws PartInitException {
		this.setSite(arg0);
		this.setInput(arg1);
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		ViewForm viewForm = new ViewForm(parent, SWT.NONE);
		viewForm.setLayout(new FillLayout());
		this.createTableView(viewForm);
		ToolBar toolBar = new ToolBar(viewForm, SWT.FLAT);
		ToolBarManager toolBarManager = new ToolBarManager(toolBar);
		viewForm.setTopLeft(toolBar);
		viewForm.setContent(tableViewer.getControl());
		toolBarManager.add(new ModifyAction());
		toolBarManager.add(new DeleteAction());
		toolBarManager.add(new RefreshAction());
		toolBarManager.update(true);
		tableViewer.setContentProvider(new TableContentProvider());
		tableViewer.setLabelProvider(new TableLabelProvider());
		tableViewer.setInput(TableDateFactory.getTableDate());
		tableViewer.setSorter(new TableSort());
	}

	@Override
	public void setFocus() {
	}

	private void createTableView(Composite parent){
		tableViewer = new TableViewer(parent, SWT.MULTI|SWT.BORDER|SWT.FULL_SELECTION);
		Table table = tableViewer.getTable();
		table.setHeaderVisible(true);

		TableColumn tableColumn = new TableColumn(table, SWT.LEFT);
		tableColumn.setText("ID");
		tableColumn.setWidth(60);
		tableColumn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sort=!sort;
				((TableSort)tableViewer.getSorter()).doSort(sort?-1:1);
				tableViewer.refresh();
			}
		});
		TableColumn tableColumn2 = new TableColumn(table, SWT.LEFT);
		tableColumn2.setText("Name");
		tableColumn2.setWidth(80);
		tableColumn2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sort=!sort;
				((TableSort)tableViewer.getSorter()).doSort(sort?-2:2);
				tableViewer.refresh();
			}
		});
		
		TableColumn tableColumn3 = new TableColumn(table, SWT.LEFT);
		tableColumn3.setText("Other");
		tableColumn3.setWidth(80);
		tableColumn3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sort=!sort;
				((TableSort)tableViewer.getSorter()).doSort(sort?-3:3);
				tableViewer.refresh();
			}
		});
		
	}

	class ModifyAction extends Action {

		public ModifyAction() {
			setToolTipText("修改信息");
			setImageDescriptor(Activator.getImageDescriptor("/icons/alt_window16.gif"));
		}
		@Override
		public void run() {
			IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
			Object obj = selection.getFirstElement();
			if(null == obj) return;
			ModifyPatientInfoDialog modifyDialog = new ModifyPatientInfoDialog(Display.getDefault().getShells()[0]);
			modifyDialog.setTableDate((TableDate)obj);
			if(modifyDialog.open() == IDialogConstants.OK_ID){
				//更新数据库
				tableViewer.refresh();
			}
		}
		
	}
	
	class DeleteAction extends Action {
		
		public DeleteAction() {
			setToolTipText("删除信息");
			setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
		}
		@Override
		public void run() {
			IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
			Object obj = selection.getFirstElement();
			if(MessageDialog.openConfirm(null, null, "真的要删除?")){
				//数据库删除
				List<?> dates = (List<?>)tableViewer.getInput();
//				dates.remove((TableDate)obj);
				tableViewer.remove(obj);
			}
		}
		
	}
	
	class RefreshAction extends Action {

		public RefreshAction() {
			setToolTipText("刷新信息");
			setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_UNDO));
		}
		@Override
		public void run() {
			tableViewer.setInput(TableDateFactory.getTableDate());
		}
	}
}

